22. Exercise: Adding the ImageView

L1 51 Adding The ImageView SC 1

In this exercise, you will replace the TextView with an ImageView that will show the right drawable based on the random number.

1. Add the dice images to the drawable folder

Add the Dice images file as explained in the previous video.

2. Replace TextView with ImageView and assign empty_dice drawable to it:

<ImageView
        android:id="@+id/dice_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/empty_dice" />

3. Replace reference to the TextView with the ImageView:

val diceImage: ImageView = findViewById(R.id.dice_image)

4. Choose the right drawable resource based on the value of randomInt:

val drawableResource = when (randomInt) {
            1 -> R.drawable.dice_1
            2 -> R.drawable.dice_2
            3 -> R.drawable.dice_3
            4 -> R.drawable.dice_4
            5 -> R.drawable.dice_5
            else -> R.drawable.dice_6
        }

5. Finally, assign the drawableResource from above to diceImage:

diceImage.setImageResource(drawableResource)

If you want to start at this step, you can download this exercise code here: Step.04-Exercise-Adding-the-ImageView.

You will find plenty of //TODO comments to help you complete this exercise, and if you get stuck, go back and watch the video again.

Once you’re done, you can check your solution against the solution we’ve provided here Step.04-Solution-Adding-the-ImageView or using this git diff.

Task Description:

Check the steps below as you implement them to complete this exercise.

Task List:

Task Feedback:

Great job!

Step.04-Solution-Adding-the-ImageView or using this git diff.